package com.group1.bookmanagement.domain;

import java.io.File;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;

import com.group1.bookmanagement.util.DateUtils;
import com.group1.bookmanagement.util.RandomCharacter;

/**
 * 密码修改记录
 */
@Entity
@Table(name = "passwordChangedRecord")
public class PasswordChangedRecord {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;
	// 用户
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "user_id")
	private User user;
	// 修改页面url
	private String url;
	// 修改页面保存路径
	private String path;
	// 页面
	@Transient
	private File page;
	// 是否完成密码修改(0表示未完成，1表示完成)
	private Integer changed = 0;
	// 创建时间
	private Date createdAt;
	// 修改时间
	private Date updatedAt;
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public File getPage() {
		if (path == null) {
			return null;
		}
		
		return new File(path);
	}

	public void setPage(HttpServletRequest request, File page) {
		this.page = page;
		String newName = RandomCharacter.randomName(page.getName());
		path = new StringBuilder(request.getSession().getServletContext().getRealPath("/temp/"))
				.append(DateUtils.getCurrentYear())
				.append(File.separator)
				.append(DateUtils.getCurrentMonth())
				.append(File.separator)
				.append(DateUtils.getCurrentDay())
				.append(File.separator)
				.append(newName)
				.toString();

		url = new StringBuilder("http://localhost:8080/BookManagement/temp/")
				.append(DateUtils.getCurrentYear())
				.append("/")
				.append(DateUtils.getCurrentMonth())
				.append("/")
				.append(DateUtils.getCurrentDay())
				.append("/")
				.append(newName)
				.toString();
		try {
			FileUtils.copyFile(page, new File(path));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Integer getChanged() {
		return changed;
	}

	public void setChanged(Integer changed) {
		this.changed = changed;
	}

	public Date getCreatedAt() {
		return createdAt;
	}

	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}

	public Date getUpdatedAt() {
		return updatedAt;
	}

	public void setUpdatedAt(Date updatedAt) {
		this.updatedAt = updatedAt;
	}

}
